package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bigkoo.pickerview.view.OptionsPickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySubSetBinding;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.sgb.kjwl.model.entity.self.SubSetListEntity;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.utils.AddressHelper;
import com.sgb.kjwl.utils.PickerHelper;
import com.sgb.kjwl.view.ui.adapter.self.carMasters.SubscriptionSetAdapter;
import com.sgb.kjwl.viewmodel.self.carMakers.SubscriptionSetModel;
import com.swgk.core.base.di.AppComponent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @author: 杨畔
 * @date: 4/8/21 5:33 PM
 * @Description: 货源订阅设置
 */
public class SubscriptionSetActivity extends AppActivity {

    @Inject
    SubscriptionSetModel mViewModel;
    ActivitySubSetBinding mBinding;
    //地址选择器
    private OptionsPickerView mAddressPicker;
    //车型选择器
    private OptionsPickerView mCarTypePicker;
    //标签列表
    private List<SubSetListEntity> mList;
    private SubscriptionSetAdapter mAdapter;
    //当前选择下标
    private int mSelectIndex;

    public static void start(Context context) {
        Intent intent = new Intent(context, SubscriptionSetActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
//        DaggerSelfComponent.builder()
//                .appComponent(appComponent)
//                .selfModule(new SelfModule(this))
//                .build()
//                .inject(this);
    }

    @Override
    protected void initView() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_sub_set);
        mBinding.setLifecycleOwner(this);
        mBinding.actionBar.setOnBackClickListener(v -> finish());
        initPicker();
        initRecycler();
        initEvent();
        initData();
    }

    private void initPicker() {
        AddressHelper.getInstance().getAllArea(new AddressHelper.AddressCallBack() {
            @Override
            public void threeData(List<AddressEntity> provinceList, List<List<AddressEntity>> cityList, List<List<List<AddressEntity>>> districtList) {
                mAddressPicker = PickerHelper.with(SubscriptionSetActivity.this).getCommonPicker(provinceList, cityList, (options1, options2, options3, v) -> {
                    AddressEntity city = cityList.get(options1).get(options2);
                    List<CarriageSelectEntity> tags = mList.get(mSelectIndex).getList();
                    //检查重复城市
                    if (mViewModel.checkRepeatCity(tags, city)) return;
                    //设置城市
                    CarriageSelectEntity entity = new CarriageSelectEntity();
                    entity.setNo(city.getNo());
                    entity.setName(city.getName());
                    tags.add(0, entity);
                    mAdapter.notifyDataSetChanged();
                });
            }
        });
    }

    private void initRecycler() {
        mList = new ArrayList<>();
        mAdapter = new SubscriptionSetAdapter(mList);
        mBinding.rvSubSet.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.bindToRecyclerView(mBinding.rvSubSet);
    }

    private void initEvent() {
        mBinding.setOnClick(this);
        mAdapter.addCallBack(parentIndex -> {
            mSelectIndex = parentIndex;
            switch (mSelectIndex) {
                case 0://发货地
                case 1://目的地
                    if (mAddressPicker != null) mAddressPicker.show();
                    break;
                case 2://车型
                    if (mCarTypePicker != null) mCarTypePicker.show();
                    break;
            }
        });
        //
        mViewModel.ldSub.observe(this, subSetEntity -> {
            mList.clear();
            mList.add(new SubSetListEntity("请选择发货地（最多三个）", subSetEntity.getCity()));
            mList.add(new SubSetListEntity("请选择目的地（最多三个）", subSetEntity.getDestCity()));
            mList.add(new SubSetListEntity("请选择车型（最多三个）", subSetEntity.getCarType()));
            mAdapter.notifyDataSetChanged();
        });
        //
        mViewModel.ldCarType.observe(this, list -> {
            if (list == null) return;
            mCarTypePicker = PickerHelper.with(this).getCommonPicker(list, (options1, options2, options3, v) -> {
                GoodsInfoTypeEntity3 type = list.get(options1);
                List<CarriageSelectEntity> tags = mList.get(mSelectIndex).getList();
                //检查重复城市
                if (mViewModel.checkRepeatType(tags, type)) return;
                //设置车型
                CarriageSelectEntity entity = new CarriageSelectEntity();
                entity.setNo(type.getNo());
                entity.setName(type.getName());
                tags.add(0, entity);
                mAdapter.notifyDataSetChanged();
            });
        });
    }

    private void initData() {
        mViewModel.findMySub(this);
        mViewModel.getCarTypeList();
    }

    @Override
    public void onMultiClick(View v) {
        if (v == mBinding.tvSubSetSubmit) {
            mViewModel.saveSub(this, mList);
        }
    }

}
